package ds.safelock.util;

/**
 * This class provides methods to convert to/from byte array
 * to/from primitive types
 * 
 * @author David Silva
 * @version 1.0
 */
public class ByteHelper {

	/**
	 * Converts an integer to his byte[] representation
	 * @param value integer to be converted
	 * @return byte array thats represents value
	 */
	public static byte[] intToByteArray(int value)
	{
		return new byte[] {
	            (byte)(value >>> 24),
	            (byte)(value >>> 16),
	            (byte)(value >>> 8),
	            (byte)value};
	}
	
	/**
	 * Converts one given byte array to an integer
	 * @param b byte array to be converted
	 * @return integer that represents b
	 */
	public static int byteArrayToInt(byte[] b)
	{
		int value = 0;
        for (int i = 0; i < 4; i++) {
            int shift = (4 - 1 - i) * 8;
            value += (b[i] & 0x000000FF) << shift;
        }
        return value;
	}
}
